<?php
/**
 * Amazon账号（基础）
 * (model)
 */

namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class AmazonAccount extends Model
{
    protected $connection = 'mysql';

    protected $table = 'b_amazon_account';

    protected $guarded = [];

    const AUTH_AT_EXPIRED = 7 * 3600 * 24;

    public static $_authStatus = [
        0 => '未授权',
        1 => '已授权',
    ];

    public static $_api = [
        0 => '停用',
        1 => '启用',
    ];

    public static $_status = [
        0 => '停用',
        1 => '启用',
    ];

    public static $_isLocal = [
        0 => '否',
        1 => '是',
    ];

    public static $_notFanEu = [];

    public static $_awsRegion = [
        'NA' => 'NA-北美区',//包含国家：美国(US),加拿大(CA),墨西哥(MX),巴西(BR)
        'EU' => 'EU-欧洲区',//包含国家：英国(UK),德国(DE),法国(FR),意大利(IT),西班牙(ES),荷兰(NL),波兰(PL),瑞典(SE),阿拉伯联合酋长国(AE)
        'FE' => 'FE-远东区',//包含国家：澳大利亚(AU),日本(JP),新加坡(SG)
    ];

    //临时。切换后无需再用
    public function amazon_area()
    {
        return $this->belongsTo(AmazonArea::class,'area_id');
    }

    public function account_country()
    {
        return $this->belongsToMany(AmazonCountry::class,
            'b_amazon_account_country',
            'account_id',
            'country_id',
            'id',
            'country_id');
    }

    public static function options($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->pluck('account','id');
    }

    public static function getAccountById($id){
        return self::query()->where('id',$id)->value('account');
    }

    public static function getIdByAccount($account){
        return self::query()->where('account',$account)->value('id');
    }

    //账号关联的所有国家
    public static function getCountriesById($id){
        return DB::table('b_amazon_account_country')->where('account_id',$id)->pluck('country_id');
    }

    //国家对应的所有账号
    public static function getAccountByCountryId($countryId){
        return DB::table('b_amazon_account_country')->where('country_id',$countryId)->pluck('account_id');
    }

    /**
     * 获取API已授权的账号ID（集合）
     * @param $countryId
     * @return array
     */
    public static function getCountryAccountIds($countryId){
        $amazonAccountIds = self::getAccountByCountryId($countryId); //同一国家下的所有账号
        $accountIds = [];
        foreach ($amazonAccountIds as $key => $accountId){
            $amazonAccount = AmazonAccount::query()->where([
                'id' => $accountId,
                'status' => 1,
                'api' => 1,
                'auth_status' => 1
            ])->first();
            if ($amazonAccount) {
                if ($amazonAccount->api == 1 && $amazonAccount->auth_status == 1) {
                    $accountIds[] = $amazonAccount->id;
                }
            }
        }
        return $accountIds;
    }

    /**
     * 获取账号
     * @param array $where
     * @return Builder[]|Collection
     */
    public static function optionsForApi($where = []){
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->where('status',1)->orderBy('id')->get();
    }

    /**
     * 获取API授权账号【账号集合】
     * @param int $accountId
     * @return Builder[]|Collection
     */
    public static function _getAccounts($accountId = 0){
        $where = $accountId ? ['id' => $accountId] : ['api' => 1, 'auth_status' => 1];
        return self::optionsForApi($where);
    }

}
